package Chapter;

/**
 * @author 郑静丽
 * @since 2025/5/18 14:10
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class DoubleColorBallSimulation {

    private static final int RED_MIN = 1;
    private static final int RED_MAX = 33;
    private static final int BLUE_MIN = 1;
    private static final int BLUE_MAX = 16;
    private static final int RED_COUNT = 6;
    private static final int PRICE_PER_BET = 2;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] userReds = null;
        int[] userBlues = null;
        int[] luckyReds;
        int luckyBlue;

        while (true) {
            displayMenu();
            System.out.print("请输入选项：");
            int choice = scanner.nextInt();

            switch (choice) {
                case 0 -> {
                    System.out.println("感谢使用，再见！");
                    scanner.close();
                    return;
                }
                case 1 -> {
                    System.out.print("请输入要购买的注数：");
                    int bets = scanner.nextInt();
                    userReds = new int[bets][RED_COUNT];
                    userBlues = new int[bets];
                    for (int i = 0; i < bets; i++) {
                        userReds[i] = generateNumbers(RED_MIN, RED_MAX, RED_COUNT);
                    }
                    for (int i = 0; i < bets; i++) {
                        userBlues[i] = (int) (Math.random() * BLUE_MAX + BLUE_MIN);
                    }

                    System.out.println("合计 " + (bets * PRICE_PER_BET) + " 元");
                    // 显示彩票号码
                    for (int i = 0; i < bets; i++) {
                        System.out.print("红球：");
                        printArray(userReds[i]);
                        System.out.println(" + 蓝球：" + userBlues[i]);
                    }
                }
                case 2 -> {
                    if (userReds == null || userBlues == null) {
                        System.out.println("请先购买彩票！");
                        break;
                    }
                    displayCurrentTime();
                    luckyReds = generateNumbers(RED_MIN, RED_MAX, RED_COUNT);
                    luckyBlue = (int) (Math.random() * BLUE_MAX + BLUE_MIN);

                    System.out.println("本期幸运号码：");
                    System.out.print("红球：");
                    printArray(luckyReds);
                    System.out.println(" + 蓝球：" + luckyBlue);

                    int totalPrize = 0;
                    System.out.println("\n中奖情况：");
                    for (int i = 0; i < userReds.length; i++) {
                        int redMatch = countMatches(userReds[i], luckyReds);
                        boolean blueMatch = (userBlues[i] == luckyBlue);
                        // 中奖判断逻辑（与原代码一致）
                        if (redMatch == 6 && blueMatch) {
                            System.out.println("第" + (i+1) + "注：一等奖！奖金5000000元");
                            totalPrize += 5000000;
                        } else if (redMatch == 6) {
                            System.out.println("第" + (i+1) + "注：二等奖！奖金100000元");
                            totalPrize += 100000;
                        } else if (redMatch == 5 && blueMatch) {
                            System.out.println("第" + (i+1) + "注：三等奖！奖金3000元");
                            totalPrize += 3000;
                        } else if (redMatch == 5 || (redMatch == 4 && blueMatch)) {
                            System.out.println("第" + (i+1) + "注：四等奖！奖金200元");
                            totalPrize += 200;
                        } else if (redMatch == 4 || (redMatch == 3 && blueMatch)) {
                            System.out.println("第" + (i+1) + "注：五等奖！奖金10元");
                            totalPrize += 10;
                        } else if (blueMatch) {
                            System.out.println("第" + (i+1) + "注：六等奖！奖金5元");
                            totalPrize += 5;
                        } else {
                            System.out.println("第" + (i+1) + "注：未中奖");
                        }
                    }
                    System.out.println("\n总共中奖：" + totalPrize + " 元");
                }
                default -> System.out.println("无效选项！");
            }
        }
    }

    private static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
            if (i != arr.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.print("]");
    }

    // 生成不重复随机数
    private static int[] generateNumbers(int min, int max, int count) {
        int[] numbers = new int[count];
        boolean[] used = new boolean[max - min + 1]; // 标记数组（索引0对应min）

        for (int i = 0; i < count; i++) {
            int num;
            do {
                num = (int) (Math.random() * (max - min + 1) + min);
            } while (used[num - min]);

            numbers[i] = num;
            used[num - min] = true;
        }

        // 升序
        for (int i = 0; i < count - 1; i++) {
            for (int j = 0; j < count - i - 1; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
        return numbers;
    }

    // 计算匹配数（双重循环对比）
    private static int countMatches(int[] user, int[] lucky) {
        int count = 0;
        for (int u : user) {
            for (int l : lucky) {
                if (u == l) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    // 显示时间
    private static void displayCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd E HH:mm:ss");
        System.out.println("---");
        System.out.println(sdf.format(new Date()));
        System.out.println("---");
    }

    // 菜单
    private static void displayMenu() {
        System.out.println("\nXXX彩票系统");
        System.out.println("---");
        System.out.println("0. 退出系统");
        System.out.println("1. 购买彩票");
        System.out.println("2. 开奖");
        System.out.println("---");
    }
}
